/* eslint-disable no-use-before-define */
/*
 * @Descripttion: 友盟统计
 * @version: 1.0.0
 * @Author: lai_hq@qq.com
 * @Date: 2022-06-20 13:30:21
 * @LastEditors: lai_hq@qq.com
 * @LastEditTime: 2022-08-05 09:26:09
 */

// import { useRoute } from 'vue-router'

// export default {
//     install(Vue, option) {

//     }
// }

// const route = useRoute()

declare global {
    // eslint-disable-next-line no-unused-vars
    interface Window {
        _czc: any
    }
}

interface OptionsProps {
    siteId: number
    environment: string[]
}

export default class Czc {
    static siteId: number

    static init(option: OptionsProps) {
        const { siteId, environment } = option
        if (environment?.includes(import.meta.env.MODE)) {
            Czc.siteId = siteId
            Czc.loadCnzScript()
            Czc.setAccount()
        }
    }

    static loadCnzScript() {
        const script = document.createElement('script')

        const span = document.createElement('span')

        span.setAttribute('id', `cnzz_stat_icon_${Czc.siteId}`)

        span.setAttribute('style', 'position:fixed;bottom:10px;right:-100px;')

        document.getElementsByTagName('body')[0].appendChild(span)

        script.type = 'text/javascript'
        // eslint-disable-next-line quotes
        script.src = `https://s4.cnzz.com/z_stat.php?id=${Czc.siteId}&show=pic`

        document.getElementsByTagName('head')[0].appendChild(script)
    }

    static setAccount() {
        // eslint-disable-next-line no-var, no-underscore-dangle
        var _czc: any = _czc || []
        _czc.push(['_setAccount', Czc.siteId])
    }
}

// class CzcHeader extends Czc {
//     constructor(option: OptionsProps) {
//         super(option)
//     }
// }

// watch(() => {
//     return route
// }, (v) => {
//     console.log('czc', v)
//     if (window._czc) {
//         const location = window.location
//         const contentUrl = location.pathname + location.hash
//         const refererUrl = '/'
//         window._czc.push(['_trackPageview', contentUrl, refererUrl])
//     }
// })

/**
 * @description: 是否自动发送页面pv统计
 * @param {*} boole 关闭 false 自动发送true，默认true
 */
function setAutoPageview(boole) {
    window._czc.push(['_setAutoPageview', boole])
}

/**
 * @description: 统计PV 浏览量
 * @param {*} contentUrl 内容页面url
 * @param {*} refererUrl 来源页面url
 * @return {*}
 */
function trackPageview(contentUrl, refererUrl) {}

/**
 * @description: 事件跟踪
 * @param {*} category 事件类别（必填） - 表示事件发生在谁身上
 * @param {*} action   事件操作 必填）- 表示访客跟元素交互的行为动作
 * @param {*} label    事件标签 - 用于更详细的描述事件
 * @param {*} value    事件值
 * @param {*} nodeid   网页中的div元素id值
 * @return {*}
 */
function trackEvent(category, action, label, value, nodeid) {}
